package com.imay.live.activity.discovery;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.live.Constants;
import com.imay.live.activity.BaseRequestActivity;
import com.imay.live.activity.dynamics.LabelFeedsDetailActivity;
import com.imay.live.fragment.dynamics.DynamicsListener;
import com.imay.live.fragment.dynamics.LinearFragment;
import com.imay.live.model.FeedModel;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.Result;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.R;

import java.util.ArrayList;
import java.util.List;

import static com.imay.live.activity.dynamics.LabelFeedsDetailActivity.POSITION;
import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * Created by liangzhenxiong on 2017/04/14 0014.
 * 个人动态页
 */

public class DynamicActivity extends BaseRequestActivity implements DynamicsListener {
    final static String TITLE = "title";
    public final static String MODEL = "FeedModel";
    public final static String MODELID = "FeedModelID";
    ImageView progress;
    View load_layout;
    TextView load_text, load_text2;
    private Animation mRotateAnimation;

    private LinearFragment linearFragment;
    private long feedid;

    public static void start(Context context, long Feedid, String title) {
        Intent starter = new Intent(context, DynamicActivity.class);
        starter.putExtra(TITLE, title);
        starter.putExtra(MODELID, Feedid);
        starter.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(starter);
    }

    public static void start(Activity context, long Feedid, String title, int position) {
        Intent starter = new Intent(context, DynamicActivity.class);
        starter.putExtra(TITLE, title);
        starter.putExtra(MODELID, Feedid);
        starter.putExtra(POSITION, position);
        starter.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(starter);
    }

    public static void start(Context context, ArrayList<FeedModel> feedModels, int position, String title) {
        Intent starter = new Intent(context, DynamicActivity.class);
        starter.putExtra(POSITION, position);
        starter.putExtra(TITLE, title);
        starter.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        starter.putParcelableArrayListExtra(LabelFeedsDetailActivity.FEEDSDATA, feedModels);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dynamicactivity_layout;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        String title = getIntent().getStringExtra(TITLE);
        feedid = getIntent().getLongExtra(MODELID, 0);
        setTitle(title);
        initLoadLayout();
        if (feedid == 0) {
            completeLoad();
            ArrayList<FeedModel> feedModels = getIntent().getParcelableArrayListExtra(LabelFeedsDetailActivity.FEEDSDATA);
            getIntent().putExtra(LinearFragment.FOLLOW, true);
            if (feedModels != null) {
                linearFragment = LinearFragment.newInstance();
                addFragment(linearFragment);
            }
            return;
        }
        ProtoRequestHelper.task(ProtoHttpFactory.getFeedInfo(feedid)).setTag(this.hashCode()).request(this);
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        try {
            Result<FeedModel> result = JSON.parseObject(data, new TypeReference<Result<FeedModel>>() {
            }.getType());
            if (result.result == CodeSuccess) {
                if (result.data != null) {
                    completeLoad();
                    if (linearFragment == null) {
                        getIntent().putExtra(MODEL, result.data);
                        getIntent().putExtra(LinearFragment.FOLLOW, true);
                        linearFragment = LinearFragment.newInstance();
                        addFragment(linearFragment);
                    } else {
                        List<Object> list = new ArrayList<>();
                        list.add(result.data);
                        linearFragment.setDatas(list);
                        linearFragment.setRefreshFinish();
                    }
                }
            } else {
                loadError(ErrorResult.getString(getApplicationContext(), result.result));
            }

        } catch (Exception e) {
            loadError(getResources().getString(R.string.str_loaderror));
        }

    }

    void initLoadLayout() {
        progress = $(R.id.progress_icon);
        load_layout = $(R.id.load_layout);
        load_text = $(R.id.load_text);
        load_text.setVisibility(View.GONE);
        load_text2 = $(R.id.load_text2);
        load_text2.setVisibility(View.GONE);
        mRotateAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.scan_rotate);
        progress.setAnimation(mRotateAnimation);
        load_layout.setVisibility(View.VISIBLE);
        progress.startAnimation(mRotateAnimation);
    }

    void completeLoad() {
        load_layout.setVisibility(View.GONE);
        progress.clearAnimation();
    }

    void loadError(String error) {
        progress.clearAnimation();
        progress.setImageResource(R.mipmap.wushuju);
        load_text.setText(error);
        load_text.setVisibility(View.VISIBLE);
    }

    void loadNetError() {
        progress.clearAnimation();
        progress.setImageResource(R.mipmap.wuwangluokeyong);
        load_text.setText(R.string.error_10001);
        load_text2.setText(R.string.str_check_net);
        load_text.setVisibility(View.VISIBLE);
        load_text2.setVisibility(View.VISIBLE);
    }

    @Override
    public void onRequestError(String command, int code, String message) {
        if (code == 10001) {
            loadNetError();
        } else {
            loadError(getResources().getString(R.string.str_loaderror));
        }
    }

    private void addFragment(Fragment loadFragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment fragment = fragmentManager.findFragmentByTag(LinearFragment.class.toString());
        if (fragment == null) {
            transaction.add(R.id.fragment_id, loadFragment, LinearFragment.class.toString());
        }
        transaction.show(loadFragment);
        transaction.commitAllowingStateLoss();
    }

    @Override
    public void scrollToPosition(int position) {

    }

    @Override
    public void loadMore() {

    }

    @Override
    public void refreshData() {
        if (feedid != 0) {
            ProtoRequestHelper.task(ProtoHttpFactory.getFeedInfo(feedid)).setTag(this.hashCode()).request(this);
        }
    }
}
